<?php

namespace app\api\validate;

use think\Validate;

class User extends Validate
{
    protected $rule = [
        'username' => 'require',
        'phone_number' => 'require|checkPhone',
        'code' => 'require|number|min:4',
        'type' => ['require','in'=>'1,2'],
        'sex' => ['require','in'=>'0,1,2'],
    ];

    protected $message = [
        'username' => '用户名不能为空',
        'phone_number' => '手机号不能为空',
        'code.require' => '短信验证码不能为空',
        'code.number' => '短信验证码必须为数字',
        'code.min' => '短信验证码长度不符合要求',
        'type.require' => '类型必须',
        'type.in' => '类型数值错误',
        'sex.require' => '性别必填',
        'sex.in' => '性别类型错误',
    ];

    protected $scene = [
        'send_code' => ['phone_number'],
        'login' => ['phone_number', 'code', 'type'],
        'update_user' => ['username', 'sex'],
    ];

    protected function checkPhone($value, $rule, $data = [])
    {
        if (!is_numeric($value)) {
            return false;
        }

        if (!preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $value)) {
            return '手机号不符合规则';
        }
        return  true;

    }
}